<template>
  <div>
    <h1>发布商品</h1>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="商品名称">
        <el-col :span="11">
          <el-input v-model="form.title"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="原价格">
        <el-col :span="11">
          <el-input v-model="form.bprice"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="折扣价">
        <el-col :span="11">
          <el-input v-model="form.price"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="商品描述">
        <el-col :span="11">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入商品描述"
            v-model="form.content">
          </el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="商品图片">
         <el-upload
          class="upload-demo"
          action="http://localhost:9090/image/upload"
          :on-remove="handleRemove"
          :on-success="handleSuccess"
          :file-list="fileList"
          list-type="picture">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>

      <el-form-item label="库存数量">
        <el-col :span="11">
          <el-input-number v-model="form.stock" :min="1" :max="1000"></el-input-number>
        </el-col>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">发布商品</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'CardAdd',
  data() {
    return {
      msg: '发布商品',
      fileList: [],
      form: {
        title: '',
        content: '',
        bprice: '',
        price: '',
        stock: 0,
        imgUrl: '',
        userId: this.$store.getters.getUser.id
      }
    }
  },
  methods: {
    handleRemove() {
      this.form.imgUrl='';
    },
    handleSuccess(res) {
      console.log(res.data);
      this.form.imgUrl = res.data;
    },
    onSubmit() {
      let vm = this;
      this.axios({
        url: 'http://localhost:9090/product/add',
        method: 'POST',
        data: vm.form
      }).then(function (res) {
        if (res.data.code == 200) {
          vm.$message.success('商品发布成功!');
          setTimeout(function () {
            vm.$router.push('cardList');
          }, 1000);
        }
      });
    }
  }
}
</script>

<style scoped>
</style>
